package thread;

import java.util.concurrent.*;

public class ThreadTest {
    private static ExecutorService pool;

    public static void main(String[] args) {

        //创建的线程数是有限的,最大61
        pool = new ThreadPoolExecutor(1, 12, 5000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(50), Executors.defaultThreadFactory(),new ThreadPoolExecutor.DiscardPolicy());

        //maximumPoolSize设置为2 ，拒绝策略为AbortPolic策略，直接抛出异常
        //pool = new ThreadPoolExecutor(1, 2, 1000, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());

        //自定义拒绝策略
/*        pool = new ThreadPoolExecutor(1, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5),
                Executors.defaultThreadFactory(), new RejectedExecutionHandler() {

					@Override
					public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
						System.out.println(r.toString()+"执行了拒绝策略");
					}
				});*/
        //自定义线程工厂
/*        pool = new ThreadPoolExecutor(2, 4, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5),
                new ThreadFactory() {

					@Override
					public Thread newThread(Runnable r) {
						System.out.println("线程"+r.hashCode()+"创建");
		                //线程命名
		                Thread th = new Thread(r,"threadPool"+r.hashCode());
		                return th;
					}
				}, new ThreadPoolExecutor.CallerRunsPolicy());*/

        for(int i=0;i<10000;i++) {
            pool.execute(new ThreadTask(i));
        }

    }

    static class ThreadTask implements Runnable{
        private int i;
        public ThreadTask(int index) {
            i = index;
        }
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()+"--"+i);
        }
    }
}
